const path = require("node:path");
const fs = require("fs");
const electron = require("electron");
const { app, BrowserWindow, shell, globalShortcut, ipcMain } = electron;
// 获取屏幕对象
const { screen: electronScreen } = electron;
// 获取数据库的方法
const db = require("./db/ELEdb.js");
// 使用electron-reloader热重载
try {
  require("electron-reloader")(module);
} catch {}
const createWindow = () => {
  const win = new BrowserWindow({
    width: 1200,
    height: 720,
    webPreferences: {
      preload: path.join(__dirname, "preload.js"), // 预加载脚本的路径
      webviewTag: true, // 启用 `<webview>` 标签，允许嵌入其他网页
      nodeIntegration: false, // 启用Node.js集成，可以在渲染进程中使用Node.js API
      contextIsolation: true,
      enableRemoteModule: false,
    },
  });
  //渲染本地html
  // win.loadFile("./electron/index.html");
  //渲染vue
  win.loadURL("http://localhost:3001");
};

app.whenReady().then(() => {
  // 获取主显示器的尺寸
  const mainScreen = electronScreen.getPrimaryDisplay();
  // 获取主显示器的宽度和高度
  const { width, height } = mainScreen.size;
  createWindow();
  app.on("activate", function () {
    // On macOS it's common to re-create a window in the app when the
    // dock icon is clicked and there are no other windows open.
    if (BrowserWindow.getAllWindows().length === 0) createWindow();
  });

  app.on("web-contents-created", (event, contents) => {
    contents.on("new-window", (event, navigationUrl) => {
      // 在新窗口中打开链接
      event.preventDefault();
      // eslint-disable-next-line no-undef
      shell.openExternal(navigationUrl);
    });
  });

  // 使用 ipcMain 处理来自渲染进程的消息，并向渲染进程发送消息：
  ipcMain.on("send-value", (event, value) => {
    //接收渲染进程的数据
    console.log("接收渲染进程的数据send-value", value);
    //将消息写入文件(配置热重载后写入文件在项目目录内会触发热重载)
    fs.writeFileSync("../data.txt", value);
    // 回复渲染进程
    event.reply("reply-channel", value);
  });
  // 使用 ipcMain 处理来自渲染进程的异步消息
  ipcMain.handle("async-message", async (event, message) => {
    // console.log("接收渲染进程的消息async-message:", message);
    const readMessage = fs.readFileSync("../data.txt");
    console.log("主进程读取文件的数据：:", readMessage.toString());
    return readMessage.toString();
  });
  // 使用 获取mysql数据
  ipcMain.handle("getMysqlData", async (event) => {
    const readMessage = await db.getMysqlData("user", event);
    console.log(70, readMessage);
    return readMessage;
  });
});
app.on("ready", () => {
  // 注册 Ctrl+R 快捷键来刷新窗口
  globalShortcut.register("CommandOrControl+R", () => {
    const currentWindow = BrowserWindow.getFocusedWindow();
    if (currentWindow) {
      // 刷新窗口
      currentWindow.reload();
    }
  });
  // 注册 F5 快捷键来刷新窗口
  globalShortcut.register("F5", () => {
    const currentWindow = BrowserWindow.getFocusedWindow();
    if (currentWindow) {
      currentWindow.reload();
    }
  });
  // 注册 F10 快捷键来打开开发者工具
  globalShortcut.register("F10", () => {
    const currentWindow = BrowserWindow.getFocusedWindow();
    if (currentWindow) {
      // 打开开发者工具
      currentWindow.webContents.openDevTools();
    }
  });
});
//关闭所有窗口时退出应用
app.on("window-all-closed", function () {
  if (process.platform !== "darwin") app.quit();
});
